﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HospitalSystem
{
    public partial class UIDM : System.Web.UI.MasterPage
    {
        public string role = "Norole";
        protected void Page_Load(object sender, EventArgs e)
        {
            String ShowValue = (string)Session["idlogin"];
            UserLabel.Text = ShowValue;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
            // Roles.DeleteCookie();
            Session.Clear();
        }

        protected void HomeLink_Click(object sender, EventArgs e)
        {
            string myrole = CheckRole(UserLabel.Text);
            if (UserLabel.Text != string.Empty)
            {
                if (myrole == "1") //ถ้า role = 1 SystemAdmin
                {
                    Response.Redirect("~/Admin/AdminManagement.aspx");
                }
                else if (myrole == "2")//ถ้า role = 2 CareAdmin
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
                else if (myrole == "3")//ถ้า role = 3 CostAdmin
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
                else if (myrole == "4")//ถ้า role = OT
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
                else if (myrole == "5")//ถ้า role = PT
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
                else if (myrole == "6")//ถ้า role =6 Prosthetis/Orthotist
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
                else if (myrole == "7")//ถ้า role = 7 nurse
                {
                    Response.Redirect("~/Nurse/NurseHomepage.aspx");
                }
                else if (myrole == "8")//ถ้า role = 8 Accountant
                {
                    Response.Redirect("~/Accountant/acthome.aspx");
                }
                else if (myrole == "9")//ถ้า role = 9 GeneralDoctor
                {
                    Response.Redirect("~/OThomepage.aspx");
                }
            }
            else
            {
                FormsAuthentication.SignOut();
                FormsAuthentication.RedirectToLoginPage();
                Session.Clear();
            }
        }
        private string CheckRole(string u)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString; // เรียกใช้ connectionstring จาก webconfig
            string comstr = "SELECT Role_id AS 'role id' FROM Role_Staff WHERE Staff_id IN(SELECT Staff_id FROM Employee WHERE Username = @username)"; // ดึงค่าของ roleID ตาม username,password
            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(comstr, con);
            SqlParameter param1;
            param1 = new SqlParameter();
            param1.ParameterName = "@username";
            param1.SqlDbType = SqlDbType.NVarChar;
            param1.Value = u;
            SqlParameter[] allparam = { param1};
            com.Parameters.AddRange(allparam);
            com.Connection.Open();
            SqlDataReader reader = com.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                role = reader.GetInt32(0).ToString();
                break;
            }
            com.Connection.Close();
            return role;
        }
        protected void AccountSetting_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Admin/UIDManagement.aspx");
        }
    }
}